<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />









  <meta name="baidu-site-verification" content="Q7ugSRSO7V" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="COOKIE的使用方法添加COOKIE值Setcookie(‘下标’,’值’,’有效时间’,’有效作用/’); 修改COOKIE值Setcookie(‘下标’，‘新值’，’有效时间’,’/’); 删除COOKIE值Setcookie(‘下标’,’空值’,’过期时间’,’/’); 查看COOKIE值 Cookie所有的值都是依赖于系统提供的$_COOKIE预定义数据，所有的COOKIE设置的值都会成">
<meta property="og:type" content="article">
<meta property="og:title" content="操作cookie、session">
<meta property="og:url" content="http://yoursite.com/php/操作cookie、session/index.html">
<meta property="og:site_name" content="chimps">
<meta property="og:description" content="COOKIE的使用方法添加COOKIE值Setcookie(‘下标’,’值’,’有效时间’,’有效作用/’); 修改COOKIE值Setcookie(‘下标’，‘新值’，’有效时间’,’/’); 删除COOKIE值Setcookie(‘下标’,’空值’,’过期时间’,’/’); 查看COOKIE值 Cookie所有的值都是依赖于系统提供的$_COOKIE预定义数据，所有的COOKIE设置的值都会成">
<meta property="og:updated_time" content="2017-04-23T14:37:22.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="操作cookie、session">
<meta name="twitter:description" content="COOKIE的使用方法添加COOKIE值Setcookie(‘下标’,’值’,’有效时间’,’有效作用/’); 修改COOKIE值Setcookie(‘下标’，‘新值’，’有效时间’,’/’); 删除COOKIE值Setcookie(‘下标’,’空值’,’过期时间’,’/’); 查看COOKIE值 Cookie所有的值都是依赖于系统提供的$_COOKIE预定义数据，所有的COOKIE设置的值都会成">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"hide","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":30},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/php/操作cookie、session/"/>





  <title> 操作cookie、session | chimps </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?dcb994042608bdb310eff950ba755ff1";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">chimps</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/php/操作cookie、session/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="chimps">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://sfault-avatar.b0.upaiyun.com/416/326/4163260007-58a1621e63a68_huge256">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="chimps">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                操作cookie、session
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-23T22:36:29+08:00">
                2017-04-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/php/" itemprop="url" rel="index">
                    <span itemprop="name">php</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a class="cloud-tie-join-count" href="/php/操作cookie、session/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count join-count" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="COOKIE的使用方法"><a href="#COOKIE的使用方法" class="headerlink" title="COOKIE的使用方法"></a>COOKIE的使用方法</h2><h3 id="添加COOKIE值"><a href="#添加COOKIE值" class="headerlink" title="添加COOKIE值"></a>添加COOKIE值</h3><p><code>Setcookie(‘下标’,’值’,’有效时间’,’有效作用/’);</code></p>
<h3 id="修改COOKIE值"><a href="#修改COOKIE值" class="headerlink" title="修改COOKIE值"></a>修改COOKIE值</h3><p><code>Setcookie(‘下标’，‘新值’，’有效时间’,’/’);</code></p>
<h3 id="删除COOKIE值"><a href="#删除COOKIE值" class="headerlink" title="删除COOKIE值"></a>删除COOKIE值</h3><p><code>Setcookie(‘下标’,’空值’,’过期时间’,’/’);</code></p>
<h3 id="查看COOKIE值"><a href="#查看COOKIE值" class="headerlink" title="查看COOKIE值"></a>查看COOKIE值</h3><blockquote>
<p>Cookie所有的值都是依赖于系统提供的$_COOKIE预定义数据，所有的COOKIE设置的值都会成为该数据中的一个元素。<br>查看cookie :使用var_dump()或者print_r();<br>如果使用特定的COOKIE的值 <code>$_COOKIE[‘下标’]</code><br><code>$_COOKIE</code>是一个可以跨页面的系统预定义变量</p>
</blockquote>
<hr>
<h2 id="SESSION的使用"><a href="#SESSION的使用" class="headerlink" title="SESSION的使用"></a>SESSION的使用</h2><h3 id="SESSION基本操作"><a href="#SESSION基本操作" class="headerlink" title="SESSION基本操作"></a>SESSION基本操作</h3><figure class="highlight php"><table><tr><td class="code"><pre><div class="line"><span class="comment">//在使用SESSION的页面中必须开启SESSION,放入当前页面的最前端</span></div><div class="line">Session_start();</div><div class="line"><span class="comment">//添加session</span></div><div class="line">$_SESSION[‘下标‘]=值</div><div class="line"><span class="comment">//修改SESSION</span></div><div class="line">$_SESSION[‘下标’]=新值</div><div class="line"><span class="comment">//删除SESSION,</span></div><div class="line"></div><div class="line">$_SESSION=<span class="keyword">ARRAy</span>();</div><div class="line"><span class="comment">//将session的值清空（只删除数据 保留类型）</span></div><div class="line"><span class="comment">//删除SESSION的值（清空SESSION文件，一般用于退出操作）</span></div><div class="line"></div><div class="line">Setcookie(session_name(),<span class="keyword">null</span>,time()<span class="number">-1</span>,’/’);</div><div class="line"><span class="comment">//删除客户端的sessionid</span></div><div class="line"></div><div class="line">$_SESSION=<span class="keyword">ARRAY</span>();</div><div class="line"><span class="comment">//清空session数据的内容</span></div><div class="line"></div><div class="line">SESSION_DESTROY();</div><div class="line"><span class="comment">//摧毁服务器中的SESSION文件</span></div><div class="line"></div><div class="line"><span class="comment">//查看全部SESSION信息</span></div><div class="line">Var_dump($_SESSION)或是使用print_r();</div><div class="line"><span class="comment">//使用其中SESSION的某一个元素</span></div><div class="line">$_SESSION[‘下标’]</div></pre></td></tr></table></figure>
<h3 id="Session的工作原理文字描述："><a href="#Session的工作原理文字描述：" class="headerlink" title="Session的工作原理文字描述："></a>Session的工作原理文字描述：</h3><blockquote>
<p>SESSION是基于COOKIE来实现的，如果COOKIE被禁用，SESSION将不能使用，原因：是SESSION的ID是通过COOKIE来传输。</p>
</blockquote>
<h4 id="第二种方式传送SESSION-id"><a href="#第二种方式传送SESSION-id" class="headerlink" title="第二种方式传送SESSION_id"></a>第二种方式传送SESSION_id</h4><p>基于URL的SESSION的会话原理</p>
<blockquote>
<p>在第一个页面获取对应的SESSION的id然后通过超级链接方式（手动和自动）将ID传送给下一个需要使用SESSION_ID的页面，这是方式就是基于URL传送的session会话。</p>
</blockquote>
<p>手动方式  </p>
<blockquote>
<p>需要在URL后面以参数的形式来传SESSION的ID<br>获取session的ID使用函数  <code>session_id();</code><br>在接收页面 接收ID时  也需要<code>session_id($_GET[‘名’])；</code>来接收，接收之后才能<code>SESSION_start();</code></p>
</blockquote>
<p>自动方式  </p>
<blockquote>
<p>需要更改配置文件中两个选项<br>第一个 是否开启自动传送<br><code>session.use_trans_sid = 0</code><br>将值设置为1.表示开启自动传送<br>0 表示关闭。<br>新版本里面新增加选择<br><code>Session.use_only_cookies=1</code>  表示只使用COOKIE传ID<br>将值设置为0.</p>
</blockquote>
<p>session是怎么工作的？</p>
<blockquote>
<p>将ID存入到cookie文件中，然后内容存放到服务器里。<br>那么禁用COOKIE的时候SESSION还能使用？<br>不能！  但是我们可以通过另一种方式实现。。。基于URL<br><figure class="highlight php"><table><tr><td class="code"><pre><div class="line">Session_id();获取session的id</div><div class="line">Session_name();获取session名</div><div class="line"></div><div class="line">session.save_handler = files  SESSION文件的存储方式。Files是以文件形式存储</div><div class="line">session.save_path = <span class="string">"c:/wamp/tmp"</span> 设定SESSION文件的存储路径</div><div class="line">session.use_cookies = <span class="number">1</span> 设定是否使用COOKIE来存储sessiongID</div><div class="line"></div><div class="line">session.use_only_cookies = <span class="number">1</span> 设定SESSION是否只用COOKIE   <span class="number">1</span>为仅用COOKIE  <span class="number">0</span> 表示不仅用</div><div class="line"></div><div class="line">session.name = PHPSESSID 设定SESSION的名</div><div class="line">Session_name();获取名</div><div class="line">session.auto_start = <span class="number">0</span>  是否自动开启SESSION</div><div class="line">设置为<span class="number">1</span>表示自动开启。</div><div class="line">如果开启会让我们的类无法使用，因为类在加载时必须在SESSION之前。</div><div class="line"></div><div class="line">session.cookie_lifetime = <span class="number">0</span> 设定SESSION使用COOKIE的有效生存时间。<span class="number">0</span>表示关闭浏览器前有效</div><div class="line"></div><div class="line">session.cookie_path = / 设定SESSION使用COOKIE的有效路径</div><div class="line"></div><div class="line">session.cookie_domain = 设置SESSION使用COOKIE的有效域名</div><div class="line">空：表示当前域名有效</div><div class="line">session.cookie_httponly =  设置SESSION使用COOKIE是否指允许使用HTTP协议访问</div><div class="line"></div><div class="line">session.serialize_handler = php SESSION串行化的方式。</div><div class="line">什么是串行化？把一个复合类型变成字符串形式。</div><div class="line"></div><div class="line">session.gc_probability = <span class="number">1</span> 垃圾回收几率的除数</div><div class="line">session.gc_divisor = <span class="number">1000</span> 垃圾回收几率的被除数</div><div class="line">session.gc_maxlifetime = <span class="number">1440</span>  session垃圾回收的最大时间</div><div class="line"></div><div class="line">session.use_trans_sid = <span class="number">0</span> SESSION是否使用传送的方式传SESSION的ID</div><div class="line"><span class="number">0</span>不传送    <span class="number">1</span>  传送</div><div class="line">session.hash_function = <span class="number">0</span> SESSION文件名的加密方式</div><div class="line"></div><div class="line">url_rewriter.tags = <span class="string">"a=href,area=href,frame=src,input=src,form=fakeentry"</span></div><div class="line">可以通过SESSION.USE_TRANS_SID传送SESSIONid的标签.</div></pre></td></tr></table></figure></p>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/php/配置/" rel="next" title="配置">
                <i class="fa fa-chevron-left"></i> 配置
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/php/视频/" rel="prev" title="视频">
                视频 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="cloud-tie-wrapper" class="cloud-tie-wrapper"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://sfault-avatar.b0.upaiyun.com/416/326/4163260007-58a1621e63a68_huge256"
               alt="chimps" />
          <p class="site-author-name" itemprop="name">chimps</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">59</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">19</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#COOKIE的使用方法"><span class="nav-number">1.</span> <span class="nav-text">COOKIE的使用方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#添加COOKIE值"><span class="nav-number">1.1.</span> <span class="nav-text">添加COOKIE值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改COOKIE值"><span class="nav-number">1.2.</span> <span class="nav-text">修改COOKIE值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除COOKIE值"><span class="nav-number">1.3.</span> <span class="nav-text">删除COOKIE值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看COOKIE值"><span class="nav-number">1.4.</span> <span class="nav-text">查看COOKIE值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SESSION的使用"><span class="nav-number">2.</span> <span class="nav-text">SESSION的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#SESSION基本操作"><span class="nav-number">2.1.</span> <span class="nav-text">SESSION基本操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Session的工作原理文字描述："><span class="nav-number">2.2.</span> <span class="nav-text">Session的工作原理文字描述：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#第二种方式传送SESSION-id"><span class="nav-number">2.2.1.</span> <span class="nav-text">第二种方式传送SESSION_id</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chimps</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  








  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  
    
    <script>
      var cloudTieConfig = {
        url: document.location.href, 
        sourceId: "",
        productKey: "48bb30d8783d4b38ba0c95e0283238ca",
        target: "cloud-tie-wrapper"
      };
    </script>
    <script src="https://img1.ws.126.net/f2e/tie/yun/sdk/loader.js"></script>
  










  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  

  

  

</body>
</html>
